//
//  NCTextView.swift
//  ZZUI
//
//  Created by 张文晏 on 2018/11/12.
//  Copyright © 2018 张文晏. All rights reserved.
//

import UIKit
import SnapKit

@IBDesignable public class NCTextView: UIView {
    
    // MARK: - Subviews
    lazy public var textView: UITextView = {
        let tv = UITextView()
        tv.textColor = UIColor(hex: 0x333333)
        tv.font = .systemFont(ofSize: 16)
        tv.delegate = self
        return tv
    }()
    lazy var numberLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .right
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = limitNumberColor
        return label
    }()
    
    // MARK: - Variables
    /// 当前字数显示颜色
    @IBInspectable public var currentNumberColor: UIColor = .init(hex: 0x333333)
    /// 限制字数显示颜色
    @IBInspectable public var limitNumberColor: UIColor = .init(hex: 0xcccccc)
    /// 超出字数限制显示颜色
    @IBInspectable public var exceedLimitColor: UIColor = .init(hex: 0xf5222d)
    /// 垂直间距
    @IBInspectable public var verticalMargin: CGFloat = 8
    /// 水平间距
    @IBInspectable public var horizontalMargin: CGFloat = 8
    /// 最大字数限制
    @IBInspectable public var maxNumber: Int = 140
    /// 超出显示字数可继续输入
    @IBInspectable public var canEditWhenExceed: Bool = true
    
    public var text: String {
        set { textView.text = text }
        get { return textView.text }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override public func awakeFromNib() {
        super.awakeFromNib()
        setupUI()
    }
    
    func setupUI() {
        addSubview(textView)
        textView.snp.makeConstraints { make in
            make.top.equalTo(verticalMargin)
            make.leading.equalTo(horizontalMargin)
            make.trailing.equalTo(-horizontalMargin)
        }
        textView.text = text
        addSubview(numberLabel)
        numberLabel.snp.makeConstraints { make in
            make.top.equalTo(textView.snp.bottom).offset(8)
            make.leading.equalTo(horizontalMargin)
            make.bottom.equalTo(-verticalMargin)
            make.trailing.equalTo(-horizontalMargin)
        }
        let str = NSMutableAttributedString(
            string: "\(textView.text.count)/",
            attributes: [.foregroundColor : currentNumberColor]
        )
        let maxNumberStr = NSAttributedString(
            string: "\(maxNumber)",
            attributes: [.foregroundColor : limitNumberColor]
        )
        str.append(maxNumberStr)
        numberLabel.attributedText = str
    }
    
}

extension NCTextView: UITextViewDelegate {
    
    public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        let newStr = ((textView.text ?? "") as NSString).replacingCharacters(in: range, with: text)
        let maxNumberStr = NSAttributedString(
            string: "\(maxNumber)",
            attributes: [.foregroundColor : limitNumberColor]
        )
        let isExceeded = newStr.count > maxNumber
        if isExceeded {
            if canEditWhenExceed {
                let str = NSMutableAttributedString(
                    string: "\(newStr.count)/",
                    attributes: [.foregroundColor : exceedLimitColor]
                )
                str.append(maxNumberStr)
                numberLabel.attributedText = str
            } else {
                return false
            }
        } else {
            let str = NSMutableAttributedString(
                string: "\(newStr.count)/",
                attributes: [.foregroundColor : currentNumberColor]
            )
            str.append(maxNumberStr)
            numberLabel.attributedText = str
        }
        return true
    }
    
}
